# Makefile for loader&kernel

# Entry point of Orange'S
# It must have the same value with 'KernelEntryPointPhyAddr' in load.inc!
ENTRYPOINT	= 0x30400

# Offset of entry point in kernel file
# It depends on ENTRYPOINT
ENTRYOFFSET	=   0x400

# Programs, flags, etc.
ASM		= nasm
CC			= gcc
LD			= ld
ASMFLAGS	= -f elf
CFLAGS		= -c
LDFLAGS	= -s -Ttext $(ENTRYPOINT)

# This Program
LOADER	= loader.bin
KERNEL		= kernel.bin
OBJS		= kernel.o start.o kliba.o string.o

# All Phony Targets
.PHONY : everything final image clean realclean all buildimg

# Default starting position
everything : $(LOADER) $(KERNEL)

all : realclean everything

final : all clean

image : final buildimg

clean :
	rm -f $(OBJS)

realclean :
	rm -f $(OBJS) $(LOADER) $(KERNEL)

# We assume that "a.img" exists in current folder
buildimg :
	sudo mount -o loop a.img a
	sudo cp -fv loader.bin a
	sudo cp -fv kernel.bin a
	sudo umount a

loader.bin : loader.asm load.inc fat12hdr.inc pm.inc
	$(ASM) $< -o $@

$(KERNEL) : $(OBJS)
	$(LD) $(LDFLAGS) $(OBJS) -o $(KERNEL)

kernel.o : kernel.asm
	$(ASM) $(ASMFLAGS) $< -o $@

start.o : start.c type.h const.h protect.h
	$(CC) $(CFLAGS) $< -o $@

kliba.o : kliba.asm
	$(ASM) $(ASMFLAGS) $< -o $@

string.o : string.asm
	$(ASM) $(ASMFLAGS) $< -o $@
